#include <iostream>
#include <string>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#define random(x) (rand() % x)
// 可以用random(x)代替rand()%x,更加方便地生成随机数
using namespace std;

string player[2] = {"玩家A", "玩家B"};
int map[100];
int playerPos[2] = {0, 0};
int gamePlayer = 0;
bool isGameOver = false;
int winner = -1;
int pausePlayer = -1;

string getLogo(int pos)
{
  string res = "□";
  if ((playerPos[0] == pos) && (playerPos[1] == pos))
  {
    res = "<>"; // A、B在同一个位置
  }
  else if (playerPos[0] == pos)
  {
    res = "Ａ";
  }
  else if (playerPos[1] == pos)
  {
    res = "Ｂ";
  }
  else
  {
    switch (map[pos]) // pos地图对应位置响应不同游戏效果
    {
    case 1:
      res = "☆"; // 幸运轮盘
      break;
    case 2:
      res = "◎"; // 地雷
      break;
    case 3:
      res = "△"; // 暂停
      break;
    case 4:
      res = "卍"; // 时空隧道
      break;
    }
  }
  return res;
}

void drowMap()
{ // 绘制地图

  for (int i = 0; i <= 29; ++i)
  {
    cout << getLogo(i);
  }
  cout << endl;
  for (int i = 30; i <= 34; ++i)
  {
    for (int j = 0; j <= 28; j++)
    {
      cout << "  ";
    }
    cout << getLogo(i) << endl;
    ;
  } // 竖方向一排
  for (int i = 64; i >= 35; i--)
  {
    cout << getLogo(i);
  }
  cout << endl;
  for (int i = 65; i <= 69; ++i)
  {
    cout << getLogo(i) << endl;
  }
  for (int i = 70; i <= 99; i++)
  {
    cout << getLogo(i);
  }
  cout << endl;
  cout << "地图说明：【☆幸运轮盘   卍时空隧道   ◎地雷   △暂停   <>玩家同处一个位置】" << endl;
}

void gameOver()
{
  isGameOver = true;
  winner = gamePlayer;
  playerPos[gamePlayer] = 99;
  system("cls");
  drowMap();
  cout << "***游戏结束！恭喜[" << player[gamePlayer] << "]取得胜利！" << endl;
  system("PAUSE");
}
void Map()
{                                                       // 初始化地图 (3)
  int luckyTurn[] = {6, 23, 40, 55, 69, 83};            // 幸运轮盘1
  int landMine[] = {5, 13, 17, 33, 38, 50, 64, 80, 94}; // 地雷2
  int pause[] = {9, 27, 60, 93};                        // 暂停3
  int timeTunnel[] = {20, 25, 45, 63, 72, 88, 90};      // 时空隧道4
  int i;
  for (i = 0; i < 6; ++i) // 循环对应效果数组数
  {
    int pos = luckyTurn[i];
    map[pos] = 1;
  }
  for (i = 0; i < 9; ++i)
  {
    int pos = landMine[i];
    map[pos] = 2;
  }
  for (i = 0; i < 4; ++i)
  {
    int pos = pause[i];
    map[pos] = 3;
  }
  for (i = 0; i < 7; ++i)
  {
    int pos = timeTunnel[i];
    map[pos] = 4;
  }
  // 将效果化定义为数字方便使用case
}
void menu()
{ // 初始化界面 (1)
  cout << "*******************小游戏*****************" << endl;
  cout << "*                                        *" << endl;
  cout << "*                骑士飞行棋              *" << endl;
  cout << "*                                        *" << endl;
  cout << "****************软工二班王翔***************" << endl;
} // 简单界面
void joinPlayer()
{ // 加入玩家 (2)
  string tmpStr = "";
  cout << "请输入玩家A的名字___" << endl;
  cin >> tmpStr;
  while (tmpStr == "")
  {
    cout << "玩家名字不能为空请重新输入___" << endl;
    cin >> tmpStr;
  }
  player[0] = "A" + tmpStr; // 利用数组分开A和B
  cout << "请输入玩家B的名字___" << endl;
  cin >> tmpStr;
  while (tmpStr == "")
  {
    cout << "玩家名字不能为空请重新输入___" << endl;
    cin >> tmpStr;
  }
  while (tmpStr == player[0])
  {
    cout << "玩家名字不能重复，请重新输入___" << endl;
    cin >> tmpStr;
  }
  player[1] = "B" + tmpStr;
  system("cls");
  cout << "***玩家加入成功..." << endl;
  cout << "***地图中[Ａ]表示玩家[" << player[0] << "]的位置..." << endl;
  cout << "***地图中[Ｂ]表示玩家[" << player[1] << "]的位置..." << endl;
}
void qidong()
{ // 投掷骰子
  short number = 0;
  while (!isGameOver)
  {
    char a;
    cout << "***请[" << player[gamePlayer] << "]输入g投掷骰子..." << endl;
    cin >> a;
    if (a == 'g')
    {
      system("cls");
      number = random(6) + 1; // 1到6随机数
      cout << "***玩家[" << player[gamePlayer] << "]投掷的骰子数为：" << number << endl;
      playerPos[gamePlayer] += number;
      int pos = playerPos[gamePlayer];
      if (pos >= 99)
      {
        gameOver(); // 在地图99以上游戏结束
      }
      else
      {
        switch (map[pos])
        {
        case 0:
          if (pausePlayer = -1)
          {
            gamePlayer = !gamePlayer;
          }
          else if (pausePlayer = 0)
          {
            pausePlayer++;
          }
          else if (pausePlayer = 1)
          {
            pausePlayer = -1;
          }
          break;
        case 1:
          int cnumber;
          cout << "***哇哦！玩家[" << player[gamePlayer] << "]获得幸运转一转的机会..." << endl;
          cout << "***请输入数字选择要进行的操作...." << endl;
          cout << "1--与对方交换位置" << endl
               << "2--轰炸对方" << endl;
          cin >> cnumber;
          // 1、2对应两种不同效果
          if (cnumber == 1)
          {
            int t = 0;
            t = playerPos[gamePlayer];
            playerPos[gamePlayer] = playerPos[!gamePlayer];
            playerPos[!gamePlayer] = t;
            // 两人互换位置
          }
          else if (cnumber == 2)
          {
            playerPos[!gamePlayer] -= 6;
            // 让对方后退6步
          }
          else
          {
            cout << "输入非规定数字！机会丢失。" << endl;
          }
          gamePlayer = !gamePlayer;
          // 这行语句的作用是换对方掷筛子
          break;
        case 2:
          cout << "***啊哦！玩家[" << player[gamePlayer] << "]踩到地雷啦，后退6步..." << endl;
          playerPos[gamePlayer] -= 6;
          gamePlayer = !gamePlayer;
          break;
        case 3:
          cout << "***悲剧呀！玩家[" << player[gamePlayer] << "]暂停投掷一次..." << endl;
          pausePlayer = 0;
          gamePlayer = !gamePlayer;
          break;
        case 4:
          cout << "***真棒！玩家[" << player[gamePlayer] << "]穿越时空隧道..." << endl;
          playerPos[gamePlayer] += 10;
          if (playerPos[gamePlayer] >= 99)
          // 防止最后一个时空隧道超过地图数导致报错
          {
            gameOver();
          }
          gamePlayer = !gamePlayer;
          break;
        }
      }
    }
    else if (a == 'a')
    {
      winner = 0;
      gameOver();
    }
    else if (a == 'b')
    {
      winner = 1;
      gameOver();
    }
    for (int i = 0; i <= 1; i++)
    {
      if (playerPos[i] < 0)
        playerPos[i] = 0;
      // 遍历玩家位置数组，若位置小于0则赋值0，防止位置出现负数
    }
    if (!isGameOver)
    {
      drowMap();
      // 游戏没结束，重新更新地图
    }
  }
}
int main()
{
  srand((unsigned)time(NULL));
  menu();
  cout << "***开始初始化玩家设置..." << endl;
  joinPlayer();
  Map();
  drowMap();
  cout << "***本场游戏开始：[" << player[0] << "] VS [" << player[1] << "]" << endl;
  gamePlayer = random(2); // A和B随机第一个掷筛子
  qidong();
  return 0;
}
